Fix issue 14808: CheckBox/RadioButton: ToggleSwitch overlaps text when RightToLeft is enabled and AutoSize is false - #14815
Conversation
…n RightToLeft is enabled and AutoSize is false
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
There was a problem hiding this comment.
Pull request overview
This PR addresses WinForms ToggleSwitch-style CheckBox/RadioButton text layout issues under RTL + constrained width by introducing explicit text layout bounds and switching ToggleSwitch text rendering to rectangle-based TextRenderer.DrawText.
Changes:
- Compute a dedicated
textBoundsregion that excludes the toggle glyph area and render text viaTextRenderer.DrawText(..., Rectangle, ..., flags). - Adjust toggle switch horizontal placement logic to account for an inset derived from border thickness.
- Update an existing unit test assertion to allow small edge tolerance when validating switch positioning.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/System.Windows.Forms/System/Windows/Forms/Rendering/CheckBox/AnimatedToggleSwitchRenderer.cs |
Adds text-bounds computation + rectangle-based text rendering with CreateTextFormatFlags, and tweaks switch X positioning. |
src/test/unit/System.Windows.Forms/System/Windows/Forms/CheckBoxTests.cs |
Loosens edge-position assertions for the toggle glyph to account for the new inset behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
| Size textSize = TextRenderer.MeasureText(Control.Text, Control.Font); | ||
| TextFormatFlags textFormatFlags = GetTextFormatFlags(Control); | ||
| Size textSize = MeasureText(Control, textFormatFlags); | ||
| Rectangle contentBounds = ToggleSwitchMetrics.GetContentBounds(Control); |
There was a problem hiding this comment.
ToggleSwitchMetrics.GetPreferredSize still measures with the default TextRenderer.MeasureText(control.Text, control.Font) flags, while this path now measures and renders with flags that honor UseMnemonic. When UseMnemonic is false, a literal & is included during rendering but can still be treated as a mnemonic marker during preferred-size calculation. As a result, an auto-sized ToggleSwitch can be too narrow and clip or wrap the text (the separate text bounds should still prevent overlap with the switch). Could preferred-size calculation use the same flags, with a regression test such as Text = "A&B" and UseMnemonic = false?

Fixes #14808
Root Cause
In ToggleSwitch mode, text rendering was position-based (DrawText with a point) rather than bounds-based.
When text was longer than available width, there was no hard text layout rectangle excluding the switch glyph area, so text could overlap the toggle or be clipped inconsistently (including mid-word truncation behavior different from Normal mode).
Proposed changes
Customer Impact
Improves text rendering reliability for ToggleSwitch-style CheckBox/RadioButton, especially with long text and constrained widths (AutoSize=false).
Regression?
Risk
Screenshots
Before
After
14808-1mp4.mp4
Test methodology
Test environment(s)
Microsoft Reviewers: Open in CodeFlow